package model

import (
	"doc-flow/docrpc/docrpc"
	"gorm.io/gorm"
)

type Flow struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null"`
	Num     int     `gorm:"type:int;not null"`
	Price   float64 `gorm:"type:decimal(10,2);not null"`
	ClassId int     `gorm:"type:int;not null"`
}

func (f *Flow) FlowAdd(db *gorm.DB) error {
	return db.Debug().Create(&f).Error
}

func (f *Flow) FindFlowByFlowName(db *gorm.DB, name string) error {
	return db.Where("flows.name=?", name).First(&f).Error
}
func FlowList(db *gorm.DB, list []*docrpc.PingList, in *docrpc.PingListRequest) []*docrpc.PingList {
	/*
		SELECT
		  flows.id AS FlowId,
		  flows.NAME AS FlowName,
		  flows.num AS FlowNum,
		  flows.price AS FlowPrice,
		  classes.`name` AS ClassName
		FROM
		  flows
		  JOIN classes ON flows.class_id = classes.id
	*/
	tx := db.Model(&Flow{}).Select("flows.id AS flow_id",
		"flows.NAME AS flow_name",
		"flows.num AS flow_num",
		"flows.price AS flow_price",
		"classes.`name` AS class_name").
		Joins("JOIN classes ON flows.class_id = classes.id").Debug()
	if in.FlowName != "" {
		tx = tx.Where("flows.name like ?", "%"+in.FlowName+"%")
	}

	if in.ClassName != "" {
		tx = tx.Where("classes.name = ?", in.ClassName)
	}

	tx.Find(&list)
	return list
}
